let THREE = require("three")
import { TweenMax } from "gsap/TweenMax"
import { OBJECTS } from "../declarations.js"
import { TOOLS } from "../tools/tools.js"
import { HELPER } from "./helper.js"

let O = OBJECTS
let groups = OBJECTS.groups
let scenes = OBJECTS.scenes
let materials = OBJECTS.materials
let fonts = OBJECTS.fonts
let actors = OBJECTS.actors
let t = TOOLS
let lights = OBJECTS.lights
let levels = OBJECTS.levels


let mainScene, uiScene, objs, models

let SCENE = {
  init: () => {
    /////////////////////////////////////////////
    //
    // organize the groups
    //
    /////////////////////////////////////////////
    
    models = OBJECTS.models

    mainScene = OBJECTS.scenes.mainScene
    uiScene = OBJECTS.scenes.uiScene
    
    SCENE.setup()
    SCENE.createGroup(objs)


    groups.root.add(actors.pointer)
    groups.followCam.add(OBJECTS.camera)

    groups.root.add(actors.cam_container)

    // groups.helpers.add(actors.ptlightaxishelper)
    groups.root.add(actors.car_container)

    // groups.helpers.add(actors.cam_pointer_helper)
    // console.log(OBJECTS.cam_helper)

    groups.lights.children.forEach((e) => {
      groups.lights.add(e)
      // console.log(e)
    })


    // groups.root.add(actors.cam_rear_helper)

    for (let key in lights) {
      groups.environment.add(lights[key])
    }
    
  },

  setup: () => {
    objs = {
      ui: {
        name: "ui",
        attachTo: uiScene,
      },
      environment: {
        name: "environment",
        attachTo: mainScene,
      },
      root: {
        name: "mainScene",
        attachTo: mainScene,
        children: {
          followCam: {
            name: "followCam",
            visible: true,
          },
          colliderMesh: {
            name: "colliderMesh",
            visible: true,
          },
          trails: {
            name: "trails",
            visible: true,
          }
        },
      },
      lights: {
        name: 'lights',
        attachTo: mainScene,
      },
      raycaster: {
        name: "raycaster",
        attachTo: mainScene,
      },
      helpers: {
        name: "helpers",
        attachTo: mainScene,
      }
    }

  },

  toggleLevels: function (val) {
    for (var key in levels) {
      if (val == levels[key].name) {
        levels[val].visible = true
      } else {
        levels[key].visible = false
      }
    }
  },

  createGroup: (obj, par) => {
    for (var key in obj) {
      var parent = par ? par : undefined
      var name
      var attachTo = obj[key].attachTo ? obj[key].attachTo : parent
      var pos = {}
      var rot = {}
      var sc = {}
      var visible = obj[key].visible === undefined ? true : obj[key].visible

      groups[key] = new THREE.Object3D()
      parent = groups[key]
      name = obj[key].name

      groups[key].name = name
      // console.log(name,visible);

      groups[key].visible = visible

      // console.error(obj[key].type,);
      attachTo.add(groups[key])

      if (obj[key].type == "level") levels[key] = groups[key]
      // if properties for position and such exists

      if (obj[key].position) {
        pos.x = obj[key].position[0]
        pos.y = obj[key].position[1]
        pos.z = obj[key].position[2]
        groups[key].position.set(pos.x, pos.y, pos.z)
      }

      if (obj[key].rotation) {
        rot.x = t.d2r(obj[key].rotation[0])
        rot.y = t.d2r(obj[key].rotation[1])
        rot.z = t.d2r(obj[key].rotation[2])
        groups[key].rotation.set(rot.x, rot.y, rot.z)
      }
      if (obj[key].scale) {
        sc.x = obj[key].scale[0]
        sc.y = obj[key].scale[1]
        sc.z = obj[key].scale[2]
        groups[key].scale.set(sc.x, sc.y, sc.z)
      }

      if (obj[key].children) {
        SCENE.createGroup(obj[key].children, parent)
      }
    }
  },
}

export { SCENE }
